package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevControllerLog;
import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.dao.mapper.ControllerLogMapper;
import cn.sciento.farm.devicemanager.dao.model.PageConditionQuery;
import cn.sciento.farm.devicemanager.service.ControllerLogService;
import cn.sciento.farm.devicemanager.service.DeviceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Walton
 * @since 2020/10/15 17:34
 */
@Service
public class ControllerLogServiceImpl implements ControllerLogService {

    @Autowired
    private ControllerLogMapper controllerLogMapper;

    @Autowired
    DeviceService deviceService;

    @Override
    public int insert(DevControllerLog controllerLog) {
        return controllerLogMapper.insert(controllerLog);
    }

    @Override
    public IPage<DevControllerLog> getControllerLog(PageConditionQuery data) {
        QueryWrapper<DevControllerLog> queryWrapper = new QueryWrapper<>();
        List<DevDevice> byAreaId = deviceService.getByAreaId(data.getAreaId());
        if (data.getControllerId() != 0) {
            queryWrapper.eq("controller_id", data.getControllerId());
        }
        if (byAreaId.size() < 1) {
            return null;
        }
        queryWrapper.ge("start_time",data.getBeginTime());
        queryWrapper.le("start_time",data.getEndTime());
        queryWrapper.orderByDesc("start_time");
        IPage<DevControllerLog> pages = controllerLogMapper.selectPage(new Page<>(data.getCurrentPage(), data.getPageSize()), queryWrapper);
        return pages;
    }
    @Override
    public int batchDeleteLogByStartTime(long startTime) {
        return controllerLogMapper.batchDeleteLogByStartTime(startTime);
    }
}
